package com.teamwings.common.exception;

import com.teamwings.common.vo.Result;
import com.teamwings.config.shiro.FeginErrorMessage;
import com.teamwings.util.JSONUtils;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

@RestControllerAdvice
@Slf4j
public class AutoFormExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(ResultException.class)
    public Result<?> handleResultException(ResultException e){
        log.error("异常信息为:{}",e);
        return Result.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(ServiceException.class)
    public Result<?> serviceException(ServiceException e){
        log.error("异常信息为:{}",e);
        return Result.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Result<?> handlerNoFoundException(Exception e) {
        log.error("异常信息为:{}",e);
        return Result.error(404, "路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(AuthenticationException.class)
    public Result<?> handleAuthenticationException(AuthenticationException e){
        log.error("异常信息为:{}",e);
        return Result.error(500, e.getMessage());
    }

  /**
   * fegin异常统一处理类
   * @param e
   * @return
   */
  @ExceptionHandler(FeignException.class)
    public Result<?> handleFeignException(FeignException e){
      FeginErrorMessage feginErrorMessage = JSONUtils.toBean(e.contentUTF8(), FeginErrorMessage.class);
      log.error("异常信息为:{}",feginErrorMessage.getTrace());
      return Result.error(500, feginErrorMessage.getMessage());
    }


    @ExceptionHandler(DuplicateKeyException.class)
    public Result<?> handleDuplicateKeyException(DuplicateKeyException e){
        log.error("异常信息为:{}",e);
        return Result.error(400, "数据库已存在该数据");
    }

    @ExceptionHandler(AuthorizationException.class)
    public Result<?> handleAuthorizationException(AuthorizationException e){
        log.error("异常信息为:{}",e);
        return Result.error(401, "没有权限，请联系管理员授权");
    }

    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e){
        log.error("异常信息为:{}",e);
        return Result.error(500, "服务器异常");
    }

    // 全局异常校验
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<?> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 然后提取错误提示信息进行返回
        String defaultMessage = objectError.getDefaultMessage();
        return Result.error(400, defaultMessage);
    }
}
